package com.lcz.audioplayer;

import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.lcz.audioplayer.bean.AudioBean;
import com.lcz.audioplayer.databinding.ActivityScreenLockBinding;


/**
 * 作者: 刘传政
 * <p>
 * 时间: 2024/4/17 0017 10:44
 * <p>
 * QQ:1052374416
 * <p>
 * 作用:锁屏页面
 * <p>
 * 注意事项:需要权限 <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
 */
public class ScreenLockActivity extends AppCompatActivity {


    private PlayerClient playerClient;
    private PlayerManagerCallback listener;
    ActivityScreenLockBinding binding;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
        binding = ActivityScreenLockBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        playerClient = PlayerClient.getInstance();
        listener = new PlayerManagerCallback() {
            @Override
            public void onProgressChange(int currentProgress, int total, String currentPositionStr, String totalDurationStr) {

            }

            @Override
            public void onPlayStart() {
                //开始播放，修改UI成暂停的按钮
                updateUi();
            }

            @Override
            public void onPlayPause() {
                updateUi();
            }

            @Override
            public void onPlayStop() {
            }

            @Override
            public void onPlayError(MediaPlayer mp, int what, int extra) {

            }

            @Override
            public void onCompletion() {

            }
        };
        playerClient.registerListener(listener);
        updateUi();
    }

    private void updateUi() {
        AudioBean audioBean = playerClient.getAudioList().get(playerClient.getCurrentIndex());
        if (audioBean != null) {
            binding.tvName.setText(audioBean.getName());
        }
        Glide.with(this)
                .load(audioBean.getImage())
                .into(binding.ivCover);
        if (playerClient.getState() == PlayerManager.STATE_PAUSE) {
            binding.playOrPauseBtn.setImageResource(R.drawable.selector_palyer_play);
        }else {
            binding.playOrPauseBtn.setImageResource(R.drawable.selector_palyer_pause);
        }

    }


    private void initWindow() {
        //注意需要做一下判断
        if (getWindow() != null) {
            Window window = getWindow();
//            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
//                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
//                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            // 锁屏的activity内部也要做相应的配置，让activity在锁屏时也能够显示，同时去掉系统锁屏。
            // 当然如果设置了系统锁屏密码，系统锁屏是没有办法去掉的
            // FLAG_DISMISS_KEYGUARD用于去掉系统锁屏页
            // FLAG_SHOW_WHEN_LOCKED使Activity在锁屏时仍然能够显示
            window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                window.getDecorView().setSystemUiVisibility(
//                        // SYSTEM_UI_FLAG_LAYOUT_STABLE保持整个View稳定，使View不会因为SystemUI的变化而做layout
//                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
//                                // SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION，开发者容易被其中的HIDE_NAVIGATION所迷惑，
//                                // 其实这个Flag没有隐藏导航栏的功能，只是控制导航栏浮在屏幕上层，不占据屏幕布局空间；
//                                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
//                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
//                                // SYSTEM_UI_FLAG_HIDE_NAVIGATION，才是能够隐藏导航栏的Flag；
//                                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
//                                // SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN，由上面可知，也不能隐藏状态栏，只是使状态栏浮在屏幕上层。
//                                View.SYSTEM_UI_FLAG_FULLSCREEN |
//                                View.SYSTEM_UI_FLAG_IMMERSIVE);
//            }
        }
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && getWindow() != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().getDecorView().setSystemUiVisibility(
                        // SYSTEM_UI_FLAG_LAYOUT_STABLE保持整个View稳定，使View不会因为SystemUI的变化而做layout
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                                // SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION，开发者容易被其中的HIDE_NAVIGATION所迷惑，
                                // 其实这个Flag没有隐藏导航栏的功能，只是控制导航栏浮在屏幕上层，不占据屏幕布局空间；
                                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                                // SYSTEM_UI_FLAG_HIDE_NAVIGATION，才是能够隐藏导航栏的Flag；
                                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                                // SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN，由上面可知，也不能隐藏状态栏，只是使状态栏浮在屏幕上层。
                                View.SYSTEM_UI_FLAG_FULLSCREEN |
                                View.SYSTEM_UI_FLAG_IMMERSIVE);
            }
        }
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int key = event.getKeyCode();
        switch (key) {
            case KeyEvent.KEYCODE_BACK: {
                return true;
            }
            case KeyEvent.KEYCODE_MENU: {
                return true;
            }
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.i("dispatchKeyEvent", event.toString());
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_POWER:
                    // 处理电源键按下事件
                    break;
                case KeyEvent.KEYCODE_HOME:
                    // 处理Home键按下事件
//                    return true;
                    break;
                // 其他按键事件...
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playerClient.unRegisterListener(listener);
    }


    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();

        finish();
    }


    private void initView() {
        SlitherFinishLayout slideLayout = findViewById(R.id.slideLayout);
        slideLayout.setOnSlitherFinishListener(new SlitherFinishLayout.OnSlitherFinishListener() {
            @Override
            public void onSlitherFinish() {
                finish();
            }
        });
        slideLayout.setTouchView(getWindow().getDecorView());
        binding.playOrPauseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果现在的状态是正在播放的,那么就暂停
                if (playerClient.isPlaying()) {
                    playerClient.pause();
                } else {
                    //如果现在的状态是非播放的,那么我们就让播放器播放节目
                    playerClient.play();
                }
            }
        });
        binding.playPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                playerClient.playPre();

            }
        });

        binding.playNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //播放下一个节目
                playerClient.playNext();
            }
        });
    }


}
